<?php
class ContactController extends Vnf_Controller_Action {
    protected $_dir;

    public function indexAction() {
        $this->setLayout(DEFAULT_LAYOUT);
        $this->prepareData();
        $this->view->translate = $translate = new Zend_Translate('tmx', BASE_DIR . '/translate/admin.tmx', 'en');
    }
    
    public function formAction() {
    }
    
    public function processAction() {
        //$allParams = $this->_request->getParams();
        $this->view->translate = $translate = new Zend_Translate('tmx', BASE_DIR . '/translate/admin.tmx', 'en');
        
        $allParams = $this->getParams();
        $submit = @$allParams['submit'];
        unset($allParams['controller']);
        unset($allParams['action']);
        unset($allParams['module']);
        unset($allParams['submit']);
        
        $mail = new Zend_Mail("UTF-8");
        $message = "";
        foreach($allParams as $param => $value):
            $message .= "<br><b>" . $translate->_($param) . "</b>";
            $message .= "<div style='margin-left:30px'>" . $value . "</div>";
        endforeach;
        
        $email = @$allParams['email'];
        $name = @$allParams['name'];
        
        if ($submit) {
            // get All attachments
            if(!empty($_FILES)) {
                foreach($_FILES as $input_name => $file):
                    $fileatt = $_FILES[$input_name]["tmp_name"]; // gives you the temporary name of the file on the server until it is renamed
                    $fileatt_type = $_FILES[$input_name]["type"]; // gives you the size of the upload in bytes
                    $fileatt_name = $_FILES[$input_name]["name"]; // gives you the name of the file they uploaded
                    
                    $dataFile = file_get_contents($fileatt);
                    $at= $mail->createAttachment($dataFile);
                    $at->type        = $fileatttype;
                    $at->disposition = Zend_Mime::DISPOSITION_ATTACHMENT;
                    //   $at->encoding    = Zend_Mime::ENCODING_8BIT;
                    $at->filename    = $fileattname;
                endforeach;
                //print_r($_FILES);
            }
            
            $conf = Vnf::getConfigs('conf');
            
            $domain = str_replace('www.', '', $_SERVER['HTTP_HOST']);
            @$mailContact = $conf->mailcontact?$conf->mailcontact:'info@'.$domain;
                
            $mail->setBodyHtml("$message");
            $mail->setFrom($email, "$name");
            $mail->addTo($mailContact, 'Contact');
            $mail->setSubject('Contact FOrm');
            try{
                $mail->send();
                $this->view->message = $this->view->translate("We've recieved your information. We'll contact you soon. Thanks.");
            } catch (Exception $ex) {
                $this->view->message = $this->view->translate("Can't send email. Please try again.");
            }
            $this->setLayout(DEFAULT_LAYOUT);
            $this->prepareData();
            $this->_helper->viewRenderer->renderScript("contact/index.phtml");
        }
        //echo $message;die;
        //$this->_redirect("/index");
    }
}